﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;

namespace Chenxin.Lib.Business.DAL
{
    /// <summary>
    /// 数据访问类t_Contract_Finance_PayedItems。
    /// </summary>
    public class ContractFinancePayedItems
    {
        public ContractFinancePayedItems()
        { }
        #region  成员方法

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public static void Add(Chenxin.Lib.Business.Entity.ContractFinancePayedItems model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into t_Contract_Finance_PayedItems(");
            strSql.Append("PayItemId,BillId,PayType,PayMoney,Memo,InputTime)");

            strSql.Append(" values (");
            strSql.Append("@PayItemId,@BillId,@PayType,@PayMoney,@Memo,@InputTime)");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "PayItemId", DbType.Guid, model.PayItemId);
            db.AddInParameter(dbCommand, "BillId", DbType.Guid, model.BillId);
            db.AddInParameter(dbCommand, "PayType", DbType.AnsiString, model.PayType);
            db.AddInParameter(dbCommand, "PayMoney", DbType.Decimal, model.PayMoney);
            db.AddInParameter(dbCommand, "Memo", DbType.AnsiString, model.Memo);
            db.AddInParameter(dbCommand, "InputTime", DbType.DateTime, model.InputTime);
            db.ExecuteNonQuery(dbCommand);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public static void Update(Chenxin.Lib.Business.Entity.ContractFinancePayedItems model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update t_Contract_Finance_PayedItems set ");
            strSql.Append("BillId=@BillId,");
            strSql.Append("PayType=@PayType,");
            strSql.Append("PayMoney=@PayMoney,");
            strSql.Append("Memo=@Memo,");
            strSql.Append("InputTime=@InputTime");
            strSql.Append(" where PayItemId=@PayItemId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "PayItemId", DbType.Guid, model.PayItemId);
            db.AddInParameter(dbCommand, "BillId", DbType.Guid, model.BillId);
            db.AddInParameter(dbCommand, "PayType", DbType.AnsiString, model.PayType);
            db.AddInParameter(dbCommand, "PayMoney", DbType.Decimal, model.PayMoney);
            db.AddInParameter(dbCommand, "Memo", DbType.AnsiString, model.Memo);
            db.AddInParameter(dbCommand, "InputTime", DbType.DateTime, model.InputTime);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public static void Delete(Guid PayItemId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from t_Contract_Finance_PayedItems ");
            strSql.Append(" where PayItemId=@PayItemId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "PayItemId", DbType.Guid, PayItemId);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 删除关联数据
        /// </summary>
        public static void DeleteByBillId(Guid BillId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from t_Contract_Finance_PayedItems ");
            strSql.Append(" where BillId=@BillId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "BillId", DbType.Guid, BillId);
            db.ExecuteNonQuery(dbCommand);
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public static Chenxin.Lib.Business.Entity.ContractFinancePayedItems GetModel(Guid PayItemId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select PayItemId,BillId,PayType,PayMoney,Memo,InputTime from t_Contract_Finance_PayedItems ");
            strSql.Append(" where PayItemId=@PayItemId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business_Search");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "PayItemId", DbType.Guid, PayItemId);
            Chenxin.Lib.Business.Entity.ContractFinancePayedItems model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select PayItemId,BillId,PayType,PayMoney,Memo,InputTime ");
            strSql.Append(" FROM t_Contract_Finance_PayedItems ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business_Search");
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("UP_GetRecordByPage");
            db.AddInParameter(dbCommand, "tblName", DbType.AnsiString, "t_Contract_Finance_PayedItems");
            db.AddInParameter(dbCommand, "fldName", DbType.AnsiString, "ID");
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            db.AddInParameter(dbCommand, "IsReCount", DbType.Boolean, 0);
            db.AddInParameter(dbCommand, "OrderType", DbType.Boolean, 0);
            db.AddInParameter(dbCommand, "strWhere", DbType.AnsiString, strWhere);
            return db.ExecuteDataSet(dbCommand);
        }*/

        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public static List<Chenxin.Lib.Business.Entity.ContractFinancePayedItems> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select PayItemId,BillId,PayType,PayMoney,Memo,InputTime ");
            strSql.Append(" FROM t_Contract_Finance_PayedItems ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<Chenxin.Lib.Business.Entity.ContractFinancePayedItems> list = new List<Chenxin.Lib.Business.Entity.ContractFinancePayedItems>();
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business_Search");
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return list;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public static Chenxin.Lib.Business.Entity.ContractFinancePayedItems ReaderBind(IDataReader dataReader)
        {
            Chenxin.Lib.Business.Entity.ContractFinancePayedItems model = new Chenxin.Lib.Business.Entity.ContractFinancePayedItems();
            object ojb;
            ojb = dataReader["PayItemId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.PayItemId = new Guid(ojb.ToString());
            }
            ojb = dataReader["BillId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.BillId = new Guid(ojb.ToString());
            }
            model.PayType = dataReader["PayType"].ToString();
            ojb = dataReader["PayMoney"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.PayMoney = (decimal)ojb;
            }
            model.Memo = dataReader["Memo"].ToString();
            ojb = dataReader["InputTime"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.InputTime = (DateTime)ojb;
            }
            return model;
        }

        #endregion  成员方法
    }
}
